<template>
  <div class="index">
    <div class="bt">
      <van-button v-if="wechatType == -1" :loading="loading" round block color="linear-gradient(to right, #4bb0ff, #6149f6)" @click="login">授权登录</van-button>
      <van-button v-if="wechatType == 1" :loading="loading" round block color="linear-gradient(to right, #4bb0ff, #6149f6)" @click="chooseproject">选择项目</van-button>
    </div>
    <van-popup v-model="showPicker" position="bottom">
      <van-picker
        title="项目"
        show-toolbar
        :columns="projectList"
        @confirm="changeProject"
        @cancel="showPicker = false"
      />
    </van-popup>
  </div>
</template>

<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from "@/vuex/mutation-types"
import { mapState, mapActions } from 'vuex';
import { author, getAllProject, getAccessToken, getTicket } from '@/api/api'
import { Notify } from 'vant'

var sha1 = require('js-sha1')
let QueryString = {
  data: {},
  Initial: function () {
    var aPairs, aTmp
    var queryString = new String(window.location.href);
    console.log('queryString',queryString)
    queryString = queryString.split('?')[1]
    aPairs = queryString ? queryString.split('&') : []
    for (let i = 0; i < aPairs.length; i++) {
      aTmp = aPairs[i].split('=')
      this.data[aTmp[0]] = aTmp[1]
    }
  },
  GetValue: function (key) {
    return this.data[key]
  }
}
export default {
  name: 'Index',
  data () {
    return {
      loading: false,
      showPicker: false,
      wechatType: -1
    }
  },
  computed: {
    ...mapState({
      projectList: state => state.app.projectList
    })
  },
  created () {
    // this.login()
  },
  methods: {
    ...mapActions([ 'setUserInfo', 'SetProject', 'SetDictItems','SetProjectList', 'SetWechatType', 'SetTicket', 'SetACCESSTOKEN']),
    // 注入配置
    ConfigFn(JsapiTicket){
      var mytimestamp = (Date.parse(new Date())) / 1000
      var mynonceStr = sha1(String(mytimestamp)).substring(0, 16)
      var mysignature = 'jsapi_ticket=' + JsapiTicket + '&noncestr=' + mynonceStr + '&timestamp=' + mytimestamp + '&url=' + window.location.href.split('#')[0]
      wx.config({
        beta: true,
        debug: true, 
        appId: 'ww36f3351ff80a627e',
        timestamp: mytimestamp, 
        nonceStr: mynonceStr, 
        signature: sha1(mysignature),
        jsApiList: [
          'scanQRCode',
          'chooseImage'
        ]
      })
      wx.error(function(res){
        console.log('config失败---')
      })
      wx.ready(function(){
      })
    },
    login () {
      QueryString.Initial()
      console.log(JSON.stringify(QueryString.data))
      let appid = QueryString.data.state.split('#')[0]
      let code =  QueryString.data.code
      // console.log(appid)
      // let appid = 'ww36f3351ff80a627e'
      this.loading = true
      author({code: code}).then(res => {
        if(res.code === 0) {
          this.$router.push({name: 'modules'})
          // const wechatType = res.main_status
          // this.wechatType = wechatType
          // this.SetWechatType(wechatType)
          // getAllProject({userid: res.id, pageNo: 1, pageSize: 100}).then(res => {
          //   let columnsProjtct = res.result.records.map(item => {
          //     return {
          //       text: item.pname,
          //       code: item.id
          //     }
          //   })
          //   this.SetProjectList(columnsProjtct)
          // })
          this.setUserInfo({
            userId: res.id,
            username: res.username,
            realname: res.realname,
            openId: res.open_id,
            token: res.token,
            work_no: res.work_no,
            appId: appid
          })
          getAccessToken().then(res => {
            this.SetACCESSTOKEN(res.access_token)
            getTicket({acess_token: res.access_token}).then(res => {
              this.SetTicket(res.ticket)
            })
          })
          // this.SetProject({id: res.projectId, name: ''})
          this.SetDictItems(res.sysAllDictItems);
          // if (wechatType === 0) {
          //   // 子项目跳转
          //   this.$router.push({name: 'modules'})
          // } else {
          //   // 主项目选择要进入的项目
          //   this.chooseproject()
          // }
          this.loading = false
          Notify({ type: 'success', message: res.message, duration: 4000 })
        } else {
          this.loading = false
          Notify({ type: 'danger', message: res.message, duration: 4000 })
        }
      })
    },
    chooseproject () {
      this.showPicker = true
    },
    changeProject (value) {
      this.SetProject({id: value.code, name: value.text})
      this.$router.push({name: 'modules'})
    }
  }
}
</script>

<style scoped>
.index{
  width: 100vw;
  height: 100vh;
  background: url('../assets/bg.jpg') center/contain;
  /* background: url('http://pic90.nipic.com/file/20160301/22615070_131915997000_2.jpg') center/contain */
}
.bt{
  width: 60%;
  height: 60px;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
